﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace StudentListToFileAndFromFile
{
    class Program
    {
        static void Main(string[] args)
        {
            //string newFolder = @"D:\Programming\С#\Training_CodPlex\Inteducation\mvdovare\StudentListToFileAndFromFile\StudentListToFileAndFromFile\initial Folder";
            //Directory.CreateDirectory(newFolder);
            string pathToNewTxtFile = @"D:\Programming\С#\Training_CodPlex\Inteducation\mvdovare\StudentListToFileAndFromFile\StudentListToFileAndFromFile\initial Folder\StudentsTxtFile.txt";
            //File.Create(pathToNewTxtFile);
            List<Student> studentsList = new List<Student>();
            Console.WriteLine("How many students you want to add?");
            int amountOfStudents = Int32.Parse(Console.ReadLine());
            for (int i = 0; i < amountOfStudents; i++)
            {
                Student newStudent = new Student();
                newStudent.AddStudentsInfo();
                studentsList.Add(newStudent);
            }

            foreach (var student in studentsList)
            {
                File.AppendAllText(pathToNewTxtFile, student.id + " ");
                File.AppendAllText(pathToNewTxtFile, student.firstName + " ");
                File.AppendAllText(pathToNewTxtFile, student.lastName + Environment.NewLine);
            }

            List<Student> listFromFile = new List<Student>();
            string StudentsFromFile = File.ReadAllText(pathToNewTxtFile);
            StudentsFromFile = StudentsFromFile.TrimEnd('\n');
            Console.WriteLine(StudentsFromFile);
            string[] separateListOfStudent = StudentsFromFile.Split('\n');
            foreach (var student in separateListOfStudent)
            {
                Student newStudent = new Student();
                string[] sepStudInfo = student.Split(' ');
                for (int i = 0; i < sepStudInfo.Length; i++)
                {
                    if (i == 0) 
                    {
                        newStudent.id = sepStudInfo[i].Trim();
                    }
                    else if (i == 1)
                    {
                        newStudent.firstName = sepStudInfo[i].Trim();
                    }
                    else
                    {
                        newStudent.lastName = sepStudInfo[i].Trim();
                    }
                }
                listFromFile.Add(newStudent);
               
            }

            Console.WriteLine();
            foreach (var element in listFromFile)
            {
                element.PrintStudentInfo();
            }

        }
    }
}
